package com.sec.android.app.myfiles.info;

import android.content.Context;

import com.sec.android.app.myfiles.MyFilesTestRunner;
import com.sec.android.app.myfiles.info.MediaFile.MediaFileInfo;

import junit.framework.Assert;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.ArrayList;


@RunWith(MyFilesTestRunner.class)
public class testMediaFile {
    private String IMAGE_TEST_PATH = "/Images";
    private String MUSIC_TEST_PATH = "/Music";
    private String VIDEO_TEST_PATH = "/Video";
    private String ARCHIVE_TEST_PATH = "/Archive";
    private String SCC_TEST_PATH = "/SCC";
    private String SOUND_TEST_PATH = "/Sound";
    private String SWF_TEST_PATH = "/swf";

    public testMediaFile() {
        initTestContentInfo();
    }

    private void initTestContentInfo() {
        mTestContents.add(new TestContentInfo(FileType.JPG, IMAGE_TEST_PATH, "0000.jpg", "image/jpeg"));
        mTestContents.add(new TestContentInfo(FileType.BMP, IMAGE_TEST_PATH, "0000.bmp", "image/bmp"));

        mTestContents.add(new TestContentInfo(FileType.MP3, MUSIC_TEST_PATH, "0000.mp3", "audio/mpeg"));
        mTestContents.add(new TestContentInfo(FileType._3GP_AUDIO, MUSIC_TEST_PATH, "0000.3gp", "audio/3gpp"));
        mTestContents.add(new TestContentInfo(FileType.MP4_A, MUSIC_TEST_PATH, "0000.mp4", "audio/mp4"));

        mTestContents.add(new TestContentInfo(FileType.AVI, VIDEO_TEST_PATH, "0000.avi", "video/avi"));
        mTestContents.add(new TestContentInfo(FileType._3GP, VIDEO_TEST_PATH, "0000.3gp", "video/3gpp"));
        mTestContents.add(new TestContentInfo(FileType.MP4, VIDEO_TEST_PATH, "0000.mp4", "video/mp4"));

        mTestContents.add(new TestContentInfo(FileType.ZIP, ARCHIVE_TEST_PATH, "0000.zip", "application/zip"));

        mTestContents.add(new TestContentInfo(FileType.SCC, SCC_TEST_PATH, "story.scc", "application/vnd.samsung.scc.storyalbum"));
        mTestContents.add(new TestContentInfo(FileType.SCC_SCRAP, SCC_TEST_PATH, "scrap.scc", "application/vnd.samsung.scc.pinall"));
    }


    @Before
    public void setUp() throws Exception {
    }

    @After
    public void tearDown() throws Exception {
    }

    private static class TestContentInfo {
        public int mType;
        public String mPath;
        public String mName;
        public String mMimeType;

        TestContentInfo(int type, String path, String name, String mimeType) {
            mType = type;
            mPath = path;
            mName = name;
            mMimeType = mimeType;

        }
    }

    private ArrayList<TestContentInfo> mTestContents = new ArrayList<>();

    @Test
    public void testMediaFileInfo() {
        for (TestContentInfo content : mTestContents) {
            String path = content.mPath + "/" + content.mName;
            MediaFileInfo info = MediaFile.getMediaFileInfo(path, (Context) null);
            if (info != null) {
                Assert.assertEquals(path + " is failed", content.mType, info.fileType);
                Assert.assertEquals(path + " is failed", content.mMimeType, info.mimeType);
            }

            info = MediaFile.getMediaFileInfo(path, true);
            if (info != null) {
                Assert.assertEquals(path + " is failed", content.mType, info.fileType);
                Assert.assertEquals(path + " is failed", content.mMimeType, info.mimeType);
            }
        }
    }

    @Test
    public void testGetMimeType() {
        for (TestContentInfo content : mTestContents) {
            String mime = MediaFile.getMimeType(content.mPath + "/" + content.mName);
            Assert.assertEquals(content.mName + " is failed", content.mMimeType, mime);

            mime = MediaFile.getMimeType(null, content.mPath + "/" + content.mName);
            Assert.assertEquals(content.mName + " is failed", content.mMimeType, mime);
        }
    }

    @Test
    public void testGetFileType() {
        for (TestContentInfo content : mTestContents) {
            String path = content.mPath + "/" + content.mName;
            int fileType = MediaFile.getFileType(path, true);
            Assert.assertEquals(path + " is failed", content.mType, fileType);

            fileType = MediaFile.getFileType(path);
            Assert.assertEquals(path + " is failed", content.mType, fileType);

            fileType = MediaFile.getFileType(path, content.mMimeType);
            Assert.assertEquals(path + " is failed", content.mType, fileType);

            fileType = MediaFile.getFileType(path, (Context) null);
            Assert.assertEquals(path + " is failed", content.mType, fileType);
        }
    }

    @Test
    public void testGetCommonShareMimeType() {
        final String mimeType = "image/jpeg";
        String ret;

        ret = MediaFile.getCommonShareMimeType(mimeType, null);
        Assert.assertEquals("image/jpeg", ret);

        ret = MediaFile.getCommonShareMimeType(mimeType, ret);
        Assert.assertEquals("image/jpeg", ret);

        ret = MediaFile.getCommonShareMimeType(mimeType, "image/bmp");
        Assert.assertEquals("image/*", ret);

        ret = MediaFile.getCommonShareMimeType(mimeType, "image/*");
        Assert.assertEquals("image/*", ret);

        ret = MediaFile.getCommonShareMimeType(mimeType, "audio/*");
        Assert.assertEquals("application/*", ret);
    }

    @Test
    public void testGetMimeCategory() {
        final String mimeType = "image/jpeg";
        String ret;

        ret = MediaFile.getMimeCategory(mimeType);
        Assert.assertEquals("image/*", ret);

        ret = MediaFile.getMimeCategory(null);
        Assert.assertEquals("application/*", ret);
    }


    @Test
    public void testGetFileTypeForMimeType() {
        for (TestContentInfo content : mTestContents) {
            int fileType = MediaFile.getFileTypeForMimeType(content.mMimeType);
            if (content.mType != fileType) {
                if (FileType.isAudioFileType(content.mType)) {
                    Assert.assertTrue("isAudioFile", FileType.isAudioFileType(fileType));
                } else if (FileType.isVideoFileType(content.mType)) {
                    Assert.assertTrue("isVideoFile", FileType.isVideoFileType(fileType));
                } else {
                    Assert.assertTrue("Fail", false);
                }
            }
        }
    }

    @Test
    public void testGetMimeTypeFileInfo() {
        for (TestContentInfo content : mTestContents) {
            ArrayList<MediaFileInfo> ret = MediaFile.getMimeTypeFileInfo(content.mMimeType);

            boolean bExist = false;
            Assert.assertNotNull(ret);
            for (MediaFileInfo info : ret) {
                if (info.fileType == content.mType) {
                    bExist = true;
                    break;
                }
            }
            Assert.assertTrue(content.mName, bExist);
        }
    }

    @Test
    public void testIsIncludedMimeType() {
        boolean actual = MediaFile.isIncludedMimeType("image/jpg", "image/*");
        Assert.assertTrue(actual);

        actual = MediaFile.isIncludedMimeType("image/jpg", "image/jpg");
        Assert.assertTrue(actual);

        actual = MediaFile.isIncludedMimeType("image/*", "image/jpg");
        Assert.assertFalse(actual);

        actual = MediaFile.isIncludedMimeType("image/jpg", "audio/*");
        Assert.assertFalse(actual);
    }

}
